import {
  Component,
  ViewChild,
  DoCheck,
  Pipe,
  PipeTransform,
  Directive,
  ElementRef,
  Input,
  ChangeDetectorRef
} from '@angular/core';

import {NavController, ActionSheetController, Events, Platform, Scroll} from 'ionic-angular';
import {Storage} from '@ionic/storage';

import _ from "lodash";

import {NetValueService} from "./netvalue.service";
import {Common} from "../../../providers/common";
import {Content} from 'ionic-angular';
import * as moment from "moment";

@Component({
  selector: 'page-gfr-netvalue',
  templateUrl: 'netvalue.html',
  providers: [NetValueService]
})
export class GfrNetValuePage implements DoCheck {
  moa_page_name: any = "page-gfr-netvalue";
  moa_page_explain: any = "估值净值";

  title_segment = "type";
  specialFileZZl = '';
  specialFileDWJZ = '';
  @ViewChild("myScroll") myScroll: Scroll;
  @ViewChild("secondScroll") secondScroll: Scroll;
  @ViewChild(Content) content: Content;

  classifyTopList = [];
  classifySecondList = [];

  segmentClassifyTop = "-1";
  segmentClassifySecond = "";

  DataGridMargin = "";

  classifyDatas = {
    "type": {
      "classifyTop": [{
        name: "全部",
        value: "-1",
        classifySecond: []
      }, {
        name: "公募",
        value: "gm",
        classifySecond: [{
          name: "股票",
          value: "gp"
        }, {
          name: "混合",
          value: "he"
        }, {
          name: "保本",
          value: "bb"
        }, {
          name: "ETF",
          value: "etf"
        }, {
          name: "LOF",
          value: "lof"
        }, {
          name: "其他指数",
          value: "qtzs"
        }, {
          name: "债券",
          value: "zq"
        }, {
          name: "货币",
          value: "hb"
        }]
      }, {
        name: "专户",
        value: "zh",
        classifySecond: [{
          name: "一对多",
          value: "yddzh"
        }, {
          name: "一对一",
          value: "ydyzh"
        }, {
          name: "社保",
          value: "sb"
        }, {
          name: "养老金",
          value: "ylj"
        }]
      }, {
        name: "QD",
        value: "qd",
        classifySecond: [{
          name: "QD公募",
          value: "qdgm"
        }, {
          name: "QD专户",
          value: "qdzh"
        }]
      }, {
        name: "外包",
        value: "wb",
        classifySecond: [{
          name: "瑞元",
          value: "ry"
        }, {
          name: "外包",
          value: "wb"
        }]
      }],
    },
    "group": {
      "classifyTop": [{
        name: "全部",
        value: "-1",
        classifySecond: []
      }, {
        name: "各组净值表",
        value: "gzjzb",
        classifySecond: [{
          name: "公募权益组",
          value: "gmqyz"
        }, {
          name: "公募债券组",
          value: "gmzqz"
        }, {
          name: "专户组",
          value: "zhz"
        }, {
          name: "公募QDII组",
          value: "gmqdiiz"
        }, {
          name: "外包组(瑞元)",
          value: "qbzry"
        }, {
          name: "外包组(外包)",
          value: "qbzwb"
        }]
      }, {
        name: "估值规模",
        value: "gzgm",
        classifySecond: []
      }]
    }
  };

  dataList = [];
  nowDataList = [];
  scrollHeight;
  nowScrollHeight;
  pushDatas = [];
  doInitClassifySecond = false;
  showSubFund = true;//是否显示子基金
  gzgmDatas = [];//估值规模数据

  isAdmin = false;
  title = "估值净值";
  isLeavePage = false;

  netValueDate = "";

  rankObject = {
    field: "",
    upRank: true
  };

  gmRankObject = {
    field: "",
    upRank: true
  };

  gzgmTotal = {
    gm: "",
    gmbd: "",
    gmbdl: "",
    num: 0,
    typeName: "合计"
  };

  typeCountTotal = {
    zcjz: 0,
    gmbd: 0
  };
  isScrollLoadMore = true;
  isLoadMore = true;

  constructor(public navCtrl: NavController,
              private actionSheetCtrl: ActionSheetController,
              private storage: Storage,
              private netValueService: NetValueService,
              public cd: ChangeDetectorRef,
              public platform: Platform,
              public events: Events) {
    this.classifyTopList = this.classifyDatas["type"]["classifyTop"];

    this.storage.get("rankObject").then(data => {
      if (data) {
        this.rankObject = data;
      }
    });

    this.storage.get("gmRankObject").then(data => {
      if (data) {
        this.gmRankObject = data;
      }
    });

    this.storage.get("configs").then(configs => {
      console.log("configs>>>>>>>>>>>>>>>", configs);
      if (configs) {
        let gfr_admin = _.find(configs, {name: "gfr_admin"});
        if (gfr_admin) {
          this.storage.get("userInfo").then(user => {
            if (user) {
              let gfr_admin_json = gfr_admin["json"];
              if (gfr_admin_json) {
                if (gfr_admin_json.indexOf(user.loginid) > -1) {
                  this.isAdmin = true;
                }
              }
            }
          })
        }
      }
    });

  }

  /**
   * [doInfinite 加载更多]
   * @param {[type]} infiniteScroll [事件]
   */
  doInfinite(infiniteScroll) {
    console.log("infiniteScroll>>>>>>");
    setTimeout(() => {
      this.doListInfinite(infiniteScroll);
    }, 500);
  }
  /**
   * [doRefresh 下拉刷新]
   * @param {[type]} refresher [事件]
   */
  doRefresh(refresher) {
    // this.pageIndex = 1;
    // this.isLoadMore = true;
    // this.loadListData(this.pageIndex, null, refresher);
  }

  ngDoCheck() {
    if (this.doInitClassifySecond) {
      this.initClassifySecond();
    }
  }

  ionViewWillLeave() {
    this.events.unsubscribe("GfrNetValuePage");
    this.isLeavePage = true;
  }

  ionViewWillEnter() {
    this.isLeavePage = false;
  }

  initClassifyTop() {
    let colNum = this.classifyTopList.length;
    if (colNum * 150 > document.getElementById("classifyTop").offsetWidth) {
      document.getElementById("classifyTop").style.width = colNum * 150 + "px";
    } else {
      document.getElementById("classifyTop").style.width = "100%";
    }
  }

  initClassifySecond() {
    let colNum = this.classifySecondList.length;
    let itemWidth = 100;
    if (colNum > 0) {
      this.DataGridMargin = "DataGridMargin";
      if (document.getElementById("classifySecond")) {
        document.getElementById("classifySecond").style.width = "100%";
        if (this.segmentClassifyTop === "gzjzb") {
          itemWidth = 150;
        }
        if (colNum * itemWidth > document.getElementById("classifySecond").offsetWidth) {
          document.getElementById("classifySecond").style.width = colNum * itemWidth + "px";
        } else {
          document.getElementById("classifySecond").style.width = "100%";
        }
      }
    } else {
      this.DataGridMargin = "";
    }
    let El_DataGridMargin = document.getElementById("DataGridMargin");
    let El_DataGridMargin2 = document.getElementById("DataGridMargin2");
    if (El_DataGridMargin) {
      El_DataGridMargin["style"]["marginTop"] = document.getElementById("HeaderDiv").offsetHeight + "px";
    }
    if (El_DataGridMargin2) {
      El_DataGridMargin2["style"]["marginTop"] = document.getElementById("HeaderDiv").offsetHeight + "px";
    }
  }

  ionViewDidEnter() {

    this.loadData("");

    this.events.subscribe("GfrNetValuePage", () => {
      this.segmentClassifyTop = "-1";
      this.classifyTopSegmentChange();
      this.loadData("");
    });
  }

  ionViewDidLoad() {
    console.log(this.myScroll);
    let scrollHeight;
    /*this.myScroll.addScrollEventListener((event) => {
      event.target.ontouchend = function () {
        console.log("---------ontouh---------");
        //   if(event.target.scrollTop<13){
        //     console.log("----------------"+event.target.scrollTop);
        //     event.target.scrollTop=0;
        //     if (this.dataList.length) {
        //       if (this.dataList.length > 15) {
        //         this.nowDataList = _.slice(this.dataList, 0, 15);
        //       } else {
        //         this.nowDataList = this.dataList;
        //       }
        //       setTimeout(()=>{
        //         event.target.scrollTop=scrollHeightStart;
        //       },2000);
        //     }
        //   }
        //   if(event.target.scrollTop==scrollHeight){
        //     this.doInfinite();
        //   }
      };
      scrollHeight = event.target.scrollHeight - this.scrollHeight;
      if (event.target.scrollTop == scrollHeight) {
        // this.doListInfinite();
      }
    });*/
  }
  initDataGridIonScroll() {
    if (this.segmentClassifyTop === 'gzgm') return;
    let contentHeight = this.content.getContentDimensions().contentHeight;
    let height = 0;
    height = contentHeight - document.getElementById("zjrqIonList").offsetHeight;
    height = height - document.getElementById("classifyTopDiv").offsetHeight;
    if (document.getElementById("classifySecond")) {
      height = height - document.getElementById("classifySecond").offsetHeight;
      console.log(document.getElementById("classifySecond").offsetHeight)

      let marginHeight=document.getElementById("classifySecondDiv").offsetHeight-document.getElementById("classifySecond").offsetHeight;
      document.getElementById("DataGridMargin").style.marginTop=-marginHeight+"px";
      console.log(marginHeight);
    } else{
      document.getElementById("DataGridMargin").style.marginTop='0';
    }
    height = height - document.getElementById("GridHeader").offsetHeight;

    // let header = document.getElementsByTagName("ion-header")[0];
    // height = height - header["offsetHeight"];
    // document.getElementById("DataGridIonScroll").style.height = height + "px";
    this.scrollHeight = height;
    console.log(document.getElementById("zjrqIonList").offsetHeight)
    console.log(contentHeight)
    console.log(document.getElementById("classifyTopDiv").offsetHeight)
    console.log("------------" + height);

    let El_DataGridMargin = document.getElementById("DataGridMargin");
    let El_DataGridMargin2 = document.getElementById("DataGridMargin2");
    if (El_DataGridMargin) {
      El_DataGridMargin["style"]["marginTop"] = document.getElementById("HeaderDiv").offsetHeight + "px";
    }
    if (El_DataGridMargin2) {
      El_DataGridMargin2["style"]["marginTop"] = document.getElementById("HeaderDiv").offsetHeight + "px";
    }
  }

  loadData(fdate) {
    this.netValueService.getGFRPushData(fdate).subscribe(result => {
      if (result) {
        console.log(result)
        this.isAdmin = result.isAdmin;
        // this.title = moment(result.date_t).format("YYYY年MM月DD日") + "估值净值";
        // console.log(this.title)

        if (!fdate) {
          this.netValueDate = moment(result.date_t).format("YYYY-MM-DD");
        }

        this.gzgmDatas = result.gzgm;
        this.pushDatas = result.data;

        _.forEach(this.dataList, data => {
          if (data["vc_secondtype"] === "货币") {
            data["fdwjz"] = data["wfsy"];
            data["rzzl"] = data["qrnhsylv"];
          }
        });
        this.dataList = _.filter(this.pushDatas, data => {

          let result = false;
          if (this.showSubFund) {
            if (data["sfzjj"] === "" || data["sfzjj"] === "Y") {
              result = true;
            }
          } else {
            if (data["sfzjj"] === "" || data["sfzjj"] === "N") {
              result = true;
            }
          }
          return result;
        });
        _.forEach(this.dataList, data => {
          if (data["fdwjz"] || data["wfsy"]) {
            if (data["fdwjz"]) {
              data["wfsy"] = data["fdwjz"]
            } else {
              data["fdwjz"] = data["wfsy"];
            }
            data["wfsy"] = parseFloat(data["wfsy"]);
            data["fdwjz"] = parseFloat(data["fdwjz"]);
          }
          if (data["rzzl"] || data["qrnhsylv"]) {
            if (data["rzzl"]) {
              data["qrnhsylv"] = data["rzzl"]
            } else {
              data["rzzl"] = data["qrnhsylv"];
            }
            data["rzzl"] = parseFloat(data["rzzl"]);
            data["qrnhsylv"] = parseFloat(data["qrnhsylv"]);
          }
          data["zcj"] = parseFloat(data["zcj"]);
          data["gmbd"] = parseFloat(data["gmbd"]);

          data["jjzzl"] = data["jjzzl"] ? parseFloat(data["jjzzl"]) : data["jjzzl"];
        });
        let gm = 0;
        let gmbd = 0;
        let gmbdl = 0;
        this.gzgmTotal.num = 0;
        _.forEach(this.gzgmDatas, gzgm => {
          gzgm["gm"] = parseFloat(gzgm["gm"]);
          gzgm["gmbd"] = parseFloat(gzgm["gmbd"]);
          this.gzgmTotal.num += gzgm["num"];
          gm += parseFloat(gzgm["gm"]);
          gmbd += parseFloat(gzgm["gmbd"]);
        });
        this.gzgmTotal.gm = _.toString(gm);
        this.gzgmTotal.gmbd = _.toString(gmbd);
        gmbdl = gm === gmbd ? 0 :
          Math.round(gmbd / (gm - gmbd) * 100);
        this.gzgmTotal.gmbdl = _.toString(gmbdl);

        if (this.rankObject.field) {
          this.dataList = _.sortBy(this.dataList, [this.rankObject.field]);
          if (!this.rankObject.upRank) {
            this.dataList = _.reverse(this.dataList);
          }
        }

        if (this.gmRankObject.field) {
          this.gzgmDatas = _.sortBy(this.gzgmDatas, [this.gmRankObject.field]);
          if (!this.gmRankObject.upRank) {
            this.gzgmDatas = _.reverse(this.gzgmDatas);
          }
        }

        this.countTotal();
        console.log(this.dataList);
        if (this.dataList.length > 0) {
          this.isLoadMore = true;
          this.isScrollLoadMore = true;
        } else {
          this.isLoadMore = false;
          this.isScrollLoadMore = false;
        }
        if (this.dataList.length) {
          if (this.dataList.length > 20) {
            this.nowDataList = _.slice(this.dataList, 0, 20);
          } else {
            this.nowDataList = this.dataList;
          }
        } else {
          this.nowDataList = [];
        }
      }
      this.initDataGridIonScroll();
    })

  }

  countTotal() {
    this.typeCountTotal.zcjz = 0;
    this.typeCountTotal.gmbd = 0;
    if (this.dataList && this.dataList.length > 0) {
      _.forEach(this.dataList, data => {
        if (data["zcj"]) {
          this.typeCountTotal.zcjz += parseFloat(data["zcj"]);
        }
        if (data["gmbd"]) {
          this.typeCountTotal.gmbd += parseFloat(data["gmbd"]);
        }
      });
    } else {
      this.typeCountTotal.zcjz = 0;
      this.typeCountTotal.gmbd = 0;
    }
  }

  rankDataList(field) {

    if (this.rankObject.field === field) {
      this.rankObject.upRank = !this.rankObject.upRank;
    } else {
      this.rankObject.field = field;
      this.rankObject.upRank = false;
    }
    this.dataList = _.sortBy(this.dataList, field);
    if (!this.rankObject.upRank) {
      this.dataList = _.reverse(this.dataList);
    }

    //每次顺序都将值设为length为15
    // this.myScroll.scrollElement.scrollTop = 0;
    if (this.dataList.length) {
      if (this.dataList.length > 20) {
        this.nowDataList = _.slice(this.dataList, 0, 20);
      } else {
        this.nowDataList = this.dataList;
      }
    } else {
      this.nowDataList = [];
    }


    console.log(this.nowDataList);
    this.storage.set("rankObject", this.rankObject);
  }

  rankGMDatas(field) {
    if (this.gmRankObject.field === field) {
      this.gmRankObject.upRank = !this.gmRankObject.upRank;
    } else {
      this.gmRankObject.field = field;
      this.gmRankObject.upRank = false;
    }

    this.gzgmDatas = _.sortBy(this.gzgmDatas, field);
    if (!this.gmRankObject.upRank) {
      this.gzgmDatas = _.reverse(this.gzgmDatas);
    }

    this.storage.set("gmRankObject", this.gmRankObject);
  }

  netValueDateChange() {
    this.loadData(moment(this.netValueDate).format("YYYYMMDD"));
  }

  titleSegmentChange() {
    this.classifyTopList = this.classifyDatas[this.title_segment]["classifyTop"];
    // this.initClassifyTop();
    this.classifySecondList = [];
    this.segmentClassifyTop = "-1";

    this.DataGridMargin = "";
    this.classifyTopSegmentChange();
    // this.initDataGridIonScroll();
    this.countTotal();
  }


  classifyTopSegmentChange() {
    this.cd.detectChanges();
    this.nowDataList = [];
    /*if (this.segmentClassifyTop != "gzgm") {
      this.myScroll.scrollElement.scrollTop = 0;
    }*/
    let second = _.find(this.classifyTopList, {value: this.segmentClassifyTop});
    console.log(second);
    if (second) {
      this.segmentClassifySecond = "";
      this.classifySecondList = second["classifySecond"];
      if (second.value === "-1") {
        this.dataList = _.filter(this.pushDatas, data => {
          let result = false;
          if (this.showSubFund) {
            if (data["sfzjj"] === "" || data["sfzjj"] === "Y") {
              result = true;
            }
          } else {
            if (data["sfzjj"] === "" || data["sfzjj"] === "N") {
              result = true;
            }
          }
          return result;
        });
      } else {
        this.dataList = _.filter(this.pushDatas, data => {
          let result = false;
          if (this.showSubFund) {
            if (data["vc_firsttype"].indexOf(second.name) > -1 && (data["sfzjj"] === "" || data["sfzjj"] === "Y")) {
              result = true;
            }
          } else {
            if (data["vc_firsttype"].indexOf(second.name) > -1 && (data["sfzjj"] === "" || data["sfzjj"] === "N")) {
              result = true;
            }
          }
          return result;
        });
      }
      if (this.dataList.length) {
        if (this.dataList.length > 20) {
          this.nowDataList = _.slice(this.dataList, 0, 20);
        } else {
          this.nowDataList = this.dataList;
        }
      }

      this.initClassifySecond();

      if (second.value === "gzjzb") {
        this.segmentClassifySecond = "gmqyz";
        let classify = _.find(second.classifySecond, {value: this.segmentClassifySecond});
        console.log(classify);
        this.classifySecondSegmentChange(classify);
      }
      this.doInitClassifySecond = true;
      this.countTotal();
    } else {
      this.classifySecondList = [];
    }

    this.cd.detectChanges();
    this.initDataGridIonScroll();
  }

  classifySecondSegmentChange(classify) {
    let isbEl = document.getElementById("isb_" + classify.value);
    if (isbEl && this.secondScroll) {
      this.secondScroll.scrollElement.scrollLeft = isbEl.offsetLeft - (this.content.contentWidth - isbEl.offsetWidth) / 2;
    }

    this.nowDataList = [];
    /*if (this.segmentClassifyTop != "gzgm") {
      this.myScroll.scrollElement.scrollTop = 0;
    }*/
    console.log(classify);
    let second = _.find(this.classifyTopList, {value: this.segmentClassifyTop});
    this.segmentClassifySecond = classify["value"];
    if (this.segmentClassifyTop != "gzjzb") {
      this.dataList = _.filter(this.pushDatas, data => {
        let result = false;
        if (this.showSubFund) {
          if ((data["vc_firsttype"].indexOf(second.name) > -1 && data["vc_secondtype"].indexOf(classify.name) > -1)
            && (data["sfzjj"] === "" || data["sfzjj"] === "Y")) {
            result = true;
          }
        } else {
          if ((data["vc_firsttype"].indexOf(second.name) > -1 && data["vc_secondtype"].indexOf(classify.name) > -1)
            && (data["sfzjj"] === "" || data["sfzjj"] === "N")) {
            result = true;
          }
        }
        return result;
      });
    } else {
      this.dataList = _.filter(this.pushDatas, data => {
        let result = false;
        if (this.showSubFund) {
          if (data["vc_group_name"].indexOf(classify.name) > -1
            && (data["sfzjj"] === "" || data["sfzjj"] === "Y")) {
            result = true;
          }
        } else {
          if (data["vc_group_name"].indexOf(classify.name) > -1
            && (data["sfzjj"] === "" || data["sfzjj"] === "N")) {
            result = true;
          }
        }
        return result;
      });
    }
    if (this.dataList.length) {
      if (this.dataList.length > 20) {
        this.nowDataList = _.slice(this.dataList, 0, 20);
      } else {
        this.nowDataList = this.dataList;
      }
    }
    this.doInitClassifySecond = false;
    this.countTotal();
  }

  moreOp() {
    let buttonText = "";
    if (this.showSubFund) {
      buttonText = "取消显示子基金";
    } else {
      buttonText = "显示子基金";
    }
    let buttons = [];
    buttons.push({
      text: buttonText,
      handler: () => {
        this.showSubFund = !this.showSubFund;
        let second = _.find(this.classifyTopList, {value: this.segmentClassifyTop});
        let classify = _.find(second.classifySecond, {value: this.segmentClassifySecond});
        if (second) {
          if (classify) {
            if (this.segmentClassifyTop != "gzjzb") {
              this.dataList = _.filter(this.pushDatas, data => {
                let result = false;
                if (this.showSubFund) {
                  if ((data["vc_firsttype"].indexOf(second.name) > -1 && data["vc_secondtype"].indexOf(classify["name"]) > -1)
                    && (data["sfzjj"] === "" || data["sfzjj"] === "Y")) {
                    result = true;
                  }
                } else {
                  if ((data["vc_firsttype"].indexOf(second.name) > -1 && data["vc_secondtype"].indexOf(classify["name"]) > -1)
                    && (data["sfzjj"] === "" || data["sfzjj"] === "N")) {
                    result = true;
                  }
                }
                return result;
              });
            } else {
              this.dataList = _.filter(this.pushDatas, data => {
                let result = false;
                if (this.showSubFund) {
                  if (data["vc_group_name"].indexOf(classify["name"]) > -1
                    && (data["sfzjj"] === "" || data["sfzjj"] === "Y")) {
                    result = true;
                  }
                } else {
                  if (data["vc_group_name"].indexOf(classify["name"]) > -1
                    && (data["sfzjj"] === "" || data["sfzjj"] === "N")) {
                    result = true;
                  }
                }
                return result;
              });
            }
          } else {
            if (this.segmentClassifyTop === "-1") {
              this.dataList = _.filter(this.pushDatas, data => {
                let result = false;
                if (this.showSubFund) {
                  if (data["sfzjj"] === "" || data["sfzjj"] === "Y") {
                    result = true;
                  }
                } else {
                  if (data["sfzjj"] === "" || data["sfzjj"] === "N") {
                    result = true;
                  }
                }
                return result;
              });
            } else {
              this.dataList = _.filter(this.pushDatas, data => {
                let result = false;
                if (this.showSubFund) {
                  if (data["vc_firsttype"].indexOf(second.name) > -1 && (data["sfzjj"] === "" || data["sfzjj"] === "Y")) {
                    result = true;
                  }
                } else {
                  if (data["vc_firsttype"].indexOf(second.name) > -1 && (data["sfzjj"] === "" || data["sfzjj"] === "N")) {
                    result = true;
                  }
                }
                return result;
              });
            }
          }
        }
        if (this.dataList.length) {
          if (this.dataList.length > 20) {
            this.nowDataList = _.slice(this.dataList, 0, 20);
          } else {
            this.nowDataList = this.dataList;
          }
        }
      }
    });
    buttons.push({
      text: "取消",
      role: 'cancel',
      handler: () => {
      }
    });

    let actionSheet = this.actionSheetCtrl.create({
      title: "",
      buttons: buttons
    });

    actionSheet.present();
  }

  flag = true;

  doListInfinite(infiniteScroll) {
    if (this.dataList.length > this.nowDataList.length) {
      if (this.dataList.length > (this.nowDataList.length + 20)) {
        let arr = _.slice(this.dataList, this.nowDataList.length, this.nowDataList.length + 20);
        this.nowDataList = _.concat(this.nowDataList, arr);
        this.isScrollLoadMore = true;
      } else {
        let arr = _.slice(this.dataList, this.nowDataList.length, this.dataList.length);
        this.nowDataList = _.concat(this.nowDataList, arr);
        this.isScrollLoadMore = false;
      }
    } else {
      this.isScrollLoadMore = false;
    }
    infiniteScroll.complete();
  }

}

/**
 * GFR数据显示格式化管道
 */
@Pipe({name: 'formatGFRDataPipe'})
export class FormatGFRDataPipe implements PipeTransform {

  constructor(public common: Common) {
  }

  transform(exponent: any, params: any): string {
    // console.log("formatGFRDataPipe>>>>>>>>>>>", params);
    if (exponent === "") {
      return "--";
    }
    let fixed = _.toString(params.currency).split(".")[1].length;
    if (typeof exponent === "number") {
      exponent = _.toString(exponent);
    }
    let val = parseFloat(exponent);
    val = val / params.unit;
    let result = (Math.round(val / params.currency) * params.currency).toFixed(fixed);
    return result + params.dw;
  }
}


@Directive({
  selector: '[dataColor]'
})
export class DataColorDirective {

  _el: any;

  data: string;

  constructor(el: ElementRef) {
    // console.log(el);
    this._el = el;
  }

  ngAfterContentInit() {
    // console.log(this._el);
    let val = parseFloat(this.data);
    if (!_.isNaN(val)) {
      if (val >= 0) {
        this._el.nativeElement.style.color = "red";
      } else {
        this._el.nativeElement.style.color = "green";
      }
    }
  }

  @Input("dataColor")
  set setData(value) {
    this.data = value;
  }

}
